
#include <core.h>

#define HEADSIGN    1
#define FILESIGN    2

ELAPI _CProfile_GetInt32(
        /* [in] */ _ELASTOS AString appName,
        /* [in] */ _ELASTOS AString keyName,
        /* [out] */ _ELASTOS Int32 * pValue);

static int CheckPolicy()
{
    int policy = 0;

    _CProfile_GetInt32("VerifyPolicy", "Policy", &policy);
    return policy;
}

ECode DoVerifyFile(const wchar_t *wszName)
{
    ECode ec = NOERROR;
    static IVerifyFile * pVerFile = NULL;
    int result;
    static int policy = 0;
    int strategy = 0;

    if(policy == 0)policy = CheckPolicy();
    if(policy == 1 || policy == 0)return NOERROR;
    if(policy == 2)strategy = FILESIGN;
    if(policy == 3)strategy = HEADSIGN;

    if(wcscmp(wszName, L"vhello.exe")==0){
        kprintf("DoVerifyFile:%S\n", wszName);
        if(pVerFile == NULL){
            ec = DzFindService(L"verifyfile", (PInterface *)&pVerFile);
            if (FAILED(ec)) goto ErrorExit;
        }

        ec = pVerFile->VerifyFile(L"d:/vhello.exe", strategy, &result);
        if (FAILED(ec)) goto ErrorExit;
        if (result != 0) goto ErrorExit;
//        kprintf("vhello signature verified ok.\n");
    }

    return NOERROR;

ErrorExit:
    return E_VERIFY_FAILED;
}

